function tagCtrl($scope, $rootScope, $modal) {
    $scope.tx_area_org = 'write something';

    $scope.tx_area = $scope.tx_area_org;
    $scope.tag_add_show = false;
    $scope.getTagList = function () {
        getTagList($scope);
    }

    $scope.deleteTag = function ($index,tagId) {
        deleteTag($scope,$index, tagId);
    }

    $scope.addTag = function(){
        addTag($scope);
    }

    $scope.showAddTag = function(){
        showAddTag($scope);
    }
}

function getTagList($scope) {
    $.ajax({
        url: '/tag/get_tag_list',
        success: function (data) {
            if (data.success) {
                $scope.$apply(function () {
                    $scope.tagList = data.tagList;
                });
            }
        },
        error: function (error) {
            console.log(error);
        }
    });
}

function deleteTag($scope,$index, tagId) {
    $.ajax({
        url: '/tag/delete_tag',
        data:{
            'tagId':tagId
        },
        success: function (data) {
            if (data.success) {
                $scope.$apply(function () {
                    $scope.tagList.splice($index, 1);
                });
            }
        },
        error: function (error) {
            console.log(error);
        }
    });
}

function showAddTag($scope){
    $scope.tx_area = $scope.tx_area_org;
    $scope.tag_add_show = !$scope.tag_add_show;
}
function addTag($scope){
    $.ajax({
        url: '/tag/add_tag',
        data:{
            'content':$scope.tx_area
        },
        success: function (data) {
            if (data.success) {
                $scope.$apply(function () {
                    $scope.tagList.splice(0,0,data.tag)
                    $scope.tag_add_show = false;
                });
            }
        },
        error: function (error) {
            console.log(error);
        }
    });
}


angular
    .module('inspinia')
    .controller('tagCtrl', tagCtrl)